package com.sea.base.json.coll

import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.sea.base.ext.global.toJsonTree
import com.sea.base.json.Json
import com.sea.base.json.Json.toJson
import java.io.Serializable

/**
 * java版链式add
 */
internal class JsonListImpl : JsonElement(), Serializable, IJsonList {

    private val list = ArrayList<Any?>()

    override fun add(obj: Any?) = this.also { list.add(obj) }

    override fun contains(obj: Any?) = list.contains(obj)

    override fun get(index: Int) = list.getOrNull(index)

    override fun toList(): List<Any?> = list

    override val size: Int
        get() = list.size

    override fun isJsonArray() = true

    override fun getAsJsonArray() = Json.toJsonTree(list) as JsonArray

    override fun deepCopy() = asJsonArray

    override fun toString() = toJson()

    override fun toJson() = toJson(list)
}